/**
 * 
 */
AmountListPage = {
		'BindEvent': function(){
			$('#btn_search').bind('click', function(){
				var page_index = 1;
				$('#content').attr('promoter_id', $('#promoter_id').val());
				AmountListPage.SearchAmountList(page_index);
				AmountListPage.SearchWithdrawAmountList(page_index);
			});
			
			
			$('#btn_updateAmount').bind('click', function(){
				if(confirm('确认要变更用户的余额么？请谨慎！')){
					var promoter_id = $('#amount_promoter_id').val();
					var amount 		= $('#amount').val();
					var amount_type = $('#amount_type').val();
					var remark = $('#remark').val();
					
					var result 		= PromoterService.UpdateAmount(promoter_id, amount, amount_type, remark);
					if(result.IsSuccess == 0){
						alert(result.ErrorMsg);
						return;
					}
					else{
						//lst_promoter_en, total_page, total_count, page_index
						alert('余额变更成功');
					}
				}
			});
			
			
			$("#div_withdraw_amount .first,.previous,.last,.next").bind('click', function(){
				if($(this).hasClass('ui-state-disabled')){
					return false;
				}
				
				var page_index = $(this).attr('page_index');
				if(page_index == undefined){
					page_index = 1;
				}
				//page_index = 1;
				AmountListPage.SearchWithdrawAmountList(page_index);
			});
			
			$("#div_amount .first,.previous,.last,.next").bind('click', function(){
				if($(this).hasClass('ui-state-disabled')){
					return false;
				}
				
				var page_index = $(this).attr('page_index');
				if(page_index == undefined){
					page_index = 1;
				}
				//page_index = 1;
				AmountListPage.SearchAmountList(page_index);
			});
		},
		'SearchAmountList': function(page_index){
			var promoter_id		= $('#content').attr('promoter_id');
			var page_size		= ServerConfig.PromoterConfig.amountList_page_search_default_size;
			
			var result = PromoterService.SearchAmountList(promoter_id, 
				page_index, page_size);

			if(result.IsSuccess == 0){
				alert(result.ErrorMsg);
				return;
			}
			else{
				//lst_promoter_en, total_page, total_count, page_index
				AmountListPage.FillAmountTable(result.Result.lst_amount_en, page_index);
				AmountListPage.SetAmountPageIndex(result.Result.total_page, result.Result.total_count, page_index);
			}
		},
		'FillAmountTable': function(lst_amount_en, page_index){
			var html_array = new Array();
			for(var index in lst_amount_en){
				var amount_en = lst_amount_en[index];
				html_array.push('<tr class="odd gradeX">');
				html_array.push('<td>' + amount_en.promoter_id + '</td>');
				html_array.push('<td>' + amount_en.promoter_name + '</td>');
				html_array.push('<td>' + amount_en.amount_kind_text + '</td>');
				html_array.push('<td>' + amount_en.amount_type_text + '</td>');
				html_array.push('<td>' + amount_en.amount + '</td>');
				html_array.push('<td>' + amount_en.begin_amount + '</td>');
				html_array.push('<td>' + amount_en.end_amount + '</td>');
				html_array.push('<td>' + amount_en.create_date + '</td>');
				html_array.push('</tr>');
			}
			
			var html = html_array.join('\r\n');
			$('#div_amount #search_result tbody').html('');
			$('#div_amount #search_result tbody').html(html);
			
			$('#div_amount #search_result').attr('page_index', page_index);;
		},
		'SetAmountPageIndex': function(total_page, total_count, page_index){
			$('#div_amount #total_count').text(total_count);
			$('#div_amount #total_page').text(total_page);
			$('#div_amount #curr_page_index').text(page_index);
			page_index = parseInt(page_index);
			
			if(page_index == 1){
				$('#div_amount .first').addClass('ui-state-disabled');
				$('#div_amount .previous').addClass('ui-state-disabled');
				$('#div_amount .first').attr('page_index', 1);
				$('#div_amount .previous').attr('page_index', 1);
			}
			else{
				$('#div_amount .first').removeClass('ui-state-disabled');
				$('#div_amount .previous').removeClass('ui-state-disabled');
				$('#div_amount .first').attr('page_index', 1);
				$('#div_amount .previous').attr('page_index', page_index - 1);
			}
	
			if(total_page == page_index){
				$('#div_amount .last').addClass('ui-state-disabled');
				$('#div_amount .next').addClass('ui-state-disabled');
				$('#div_amount .last').attr('page_index', total_page);
				$('#div_amount .next').attr('page_index', total_page);
			}
			else{
				$('#div_amount .last').removeClass('ui-state-disabled');
				$('#div_amount .next').removeClass('ui-state-disabled');
				$('#div_amount .last').attr('page_index', total_page);
				$('#div_amount .next').attr('page_index', page_index + 1);
			}
		},
		
		
		
		
		'SearchWithdrawAmountList': function(page_index){
			var promoter_id		= $('#content').attr('promoter_id');
			var page_size		= ServerConfig.PromoterConfig.withdrawAmountList_page_search_default_size;
			
			var result = PromoterService.SearchWithdrawAmountList(promoter_id, 
				page_index, page_size);

			if(result.IsSuccess == 0){
				alert(result.ErrorMsg);
				return;
			}
			else{
				//lst_promoter_en, total_page, total_count, page_index
				AmountListPage.FillWithdrawAmountTable(result.Result.lst_amount_en, page_index);
				AmountListPage.SetWithdrawAmountPageIndex(result.Result.total_page, result.Result.total_count, page_index);
			}
		},
		'FillWithdrawAmountTable': function(lst_amount_en, page_index){
			var html_array = new Array();
			for(var index in lst_amount_en){
				var amount_en = lst_amount_en[index];
				html_array.push('<tr class="odd gradeX">');
				html_array.push('<td>' + amount_en.promoter_id + '</td>');
				html_array.push('<td>' + amount_en.promoter_name + '</td>');
				html_array.push('<td>' + amount_en.amount_kind_text + '</td>');
				html_array.push('<td>' + amount_en.amount_type_text + '</td>');
				html_array.push('<td>' + amount_en.withdraw_amount + '</td>');
				html_array.push('<td>' + amount_en.begin_withdraw_amount + '</td>');
				html_array.push('<td>' + amount_en.end_withdraw_amount + '</td>');
				html_array.push('<td>' + amount_en.create_date + '</td>');
				html_array.push('</tr>');
			}
			
			var html = html_array.join('\r\n');
			$('#div_withdraw_amount #search_result tbody').html('');
			$('#div_withdraw_amount #search_result tbody').html(html);
			
			$('#div_withdraw_amount #search_result').attr('page_index', page_index);;
		},
		'SetWithdrawAmountPageIndex': function(total_page, total_count, page_index){
			$('#div_withdraw_amount #total_count').text(total_count);
			$('#div_withdraw_amount #total_page').text(total_page);
			$('#div_withdraw_amount #curr_page_index').text(page_index);
			page_index = parseInt(page_index);
			
			if(page_index == 1){
				$('#div_withdraw_amount .first').addClass('ui-state-disabled');
				$('#div_withdraw_amount .previous').addClass('ui-state-disabled');
				$('#div_withdraw_amount .first').attr('page_index', 1);
				$('#div_withdraw_amount .previous').attr('page_index', 1);
			}
			else{
				$('#div_withdraw_amount .first').removeClass('ui-state-disabled');
				$('#div_withdraw_amount .previous').removeClass('ui-state-disabled');
				$('#div_withdraw_amount .first').attr('page_index', 1);
				$('#div_withdraw_amount .previous').attr('page_index', page_index - 1);
			}
	
			if(total_page == page_index){
				$('#div_withdraw_amount .last').addClass('ui-state-disabled');
				$('#div_withdraw_amount .next').addClass('ui-state-disabled');
				$('#div_withdraw_amount .last').attr('page_index', total_page);
				$('#div_withdraw_amount .next').attr('page_index', total_page);
			}
			else{
				$('#div_withdraw_amount .last').removeClass('ui-state-disabled');
				$('#div_withdraw_amount .next').removeClass('ui-state-disabled');
				$('#div_withdraw_amount .last').attr('page_index', total_page);
				$('#div_withdraw_amount .next').attr('page_index', page_index + 1);
			}
		},

};


$(document).ready(function(){
	if($('#content').attr('promoter_id') != ''){
		var page_index = 1;
		AmountListPage.SearchAmountList(page_index);
		AmountListPage.SearchWithdrawAmountList(page_index);
	}
	AmountListPage.BindEvent();
});